package com.github.live.plugins.base;

import androidx.collection.ArraySet;

import com.github.livebase.EpgInformationUpdater;
import com.github.livebase.bean.ChannelGroup;
import com.github.livebase.event.Event;
import com.github.livebase.event.EventListener;
import com.github.livebase.plugins.EpgUpdaterRegister;
import com.github.livebase.plugins.PluginContext;
import com.github.livebase.util.LiveContext;
import com.github.livebase.util.base.CollectionsUtil;

import java.util.List;
import java.util.Set;

/**
 * @ClassName EpgRegister
 * @description:
 * @author: biao532
 * @create: 2024-01-21 16:51
 * @Version 1.0
 **/
public class EpgRegister implements EpgUpdaterRegister, EventListener {
    private EpgInformationUpdater updater;
    @Override
    public EpgInformationUpdater register(LiveContext liveContext, PluginContext context) {
        if (updater == null) {
            List<ChannelGroup> groups = liveContext.getPluginFactory().getGroups();
            Set<String> set = new ArraySet<>();
            CollectionsUtil.foreach(groups, (g) -> {
                CollectionsUtil.foreach(g.getChannels(), (c)-> {
                    set.add(c.getId());
                });
            });
            updater = new TodayUpdater(liveContext.getLiveDatabaseHelper().getEpg(), set);
        }
        return updater;
    }

    @Override
    public void onEvent(Event event) {
        if (updater != null) {
            updater.onEvent(event);
        }
    }
}
